import { createStore } from "vuex";
import { roles } from "@/api/article.js";
import { asyncRouter } from "../router";
export default createStore({
  state: {
    userName: "",
    routers: [],
    websocket: null,
  },
  mutations: {
    setUserName(state, data) {
      state.userName = data;
    },
    setRouters(state, data) {
      state.routers = data;
    },
    setWebsocket(state, data) {
      state.websocket = data;
    },
  },
  actions: {
    setUserName({ commit }, data) {
      commit("setUserName", data);
    },
    getUserName({ commit }) {
      return new Promise((resolve, reject) => {
        roles()
          .then((res) => {
            if (res.code == 200) {
              if (res.data == "admin") {
                commit("setRouters", asyncRouter);
              }
              commit("setUserName", res.data);
              resolve(res.data);
            } else {
              resolve("");
            }
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
  modules: {},
});
